package main

import (
	"encoding/json"
	"fmt"
	"github.com/songzhibin97/gkit/cache/local_cache"
	"strconv"
	"strings"
	"studys/ThirdPartyLibrary/local_cache/cache"
	"time"
)

var (
	BlackCache local_cache.Cache
)

func main() {
	type Person struct {
		Name string
		Age  int
	}

	p := Person{
		Name: "Alice",
		Age:  25,
	}

	// 将结构体编码为 JSON 格式的字节数组
	data, err := json.Marshal(p)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	// 将字节数组转换为字符串
	jsonStr := string(data)
	fmt.Println(jsonStr)
	dr, _ := ParseDuration("7d")
	BlackCache := cache.NewLocalCache("./blackcache", dr)
	v, ok := BlackCache.Get("aaa")
	fmt.Println("Get Value:", v, "   ", ok)

	//v, ok = BlackCache.Get("bbb")
	//fmt.Println("Get Value:", v, "   ", ok)
	//BlackCache.Set("aaa", data, time.Second*time.Duration(3600))
	//BlackCache.Set("bbb", "asas", time.Second*time.Duration(3600))
	BlackCache.SaveFile("./blackcache")

}

//	func main() {
//		dr, err := ParseDuration("7d")
//		if err != nil {
//			panic(err)
//		}
//		_, err = ParseDuration("1d")
//		if err != nil {
//			panic(err)
//		}
//
//		BlackCache = local_cache.NewCache(
//			local_cache.SetDefaultExpire(dr),
//		)
//		err = BlackCache.LoadFile("./black_cache")
//		if err != nil {
//			return
//		}
//		v, ok := BlackCache.Get("aaa")
//		fmt.Println("Get Value:", v, "   ", ok)
//		//BlackCache.Set("aaa", "value", time.Second*time.Duration(3600))
//		//err = BlackCache.SaveFile("./black_cache")
//		//if err != nil {
//		//	fmt.Println(err)
//		//	return
//		//}
//	}
func ParseDuration(d string) (time.Duration, error) {
	d = strings.TrimSpace(d)
	dr, err := time.ParseDuration(d)
	if err == nil {
		return dr, nil
	}
	if strings.Contains(d, "d") {
		index := strings.Index(d, "d")

		hour, _ := strconv.Atoi(d[:index])
		dr = time.Hour * 24 * time.Duration(hour)
		ndr, err := time.ParseDuration(d[index+1:])
		if err != nil {
			return dr, nil
		}
		return dr + ndr, nil
	}

	dv, err := strconv.ParseInt(d, 10, 64)
	return time.Duration(dv), err
}
